package View;

import javax.swing.*;

import java.awt.GridLayout;
import java.awt.Color;
import java.util.ArrayList;
import Model.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import Controller.GerenciadorAplicacoes;
import Model.Categoria;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class TelaSubCategoria extends JPanel{
	JPanel sub;
	static GerenciadorAplicacoes gapp;
	static Sub_Categoria sc;
	public static JComboBox cbcategoria;
	JTextField cnome;
	JTextArea cdes;
	private JButton btnSalvar;
	private JButton btnCancelar;
	private JButton btnLimpar;
	static JScrollPane subcategoria;
	
	public TelaSubCategoria() {
		
		
		subcategoria = new JScrollPane();
		subcategoria.getViewport().setBackground(new Color(100, 175, 239));
		setLayout(new GridLayout(0, 1, 0, 0));
		add(subcategoria);
		
		sub = new JPanel();
		sub.setBackground(null);
		subcategoria.setViewportView(sub);
		
		sub.setBorder(BorderFactory.createTitledBorder("CADASTRAR SUB-CATEGORIA"));
		sub.setBackground((Color) null);
		
		JLabel label = new JLabel("CATEGORIA");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		
		JLabel label_1 = new JLabel("NOME");
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		
		JLabel label_2 = new JLabel("DESCRI\u00C7\u00C3O");
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		
		JScrollPane scrollPane = new JScrollPane();
		
		cbcategoria = new JComboBox();
		AtualizarCategoria();
		
		cnome = new JTextField();
		
		cdes = new JTextArea();
		scrollPane.setViewportView(cdes);
		
		btnSalvar = new JButton("SALVAR");
		btnSalvar.setBackground(new Color(255, 255, 255));
		btnSalvar.setForeground(new Color(0, 175, 239));
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				salvar();
				TelaAplicacoes.AbrirEstoque();
			}
		});
		
		btnCancelar = new JButton("CANCELAR");
		btnCancelar.setBackground(new Color(255, 255, 255));
		btnCancelar.setForeground(new Color(0, 175, 239));
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TelaAplicacoes.AbrirEstoque();
			}
		});
		
		btnLimpar = new JButton("LIMPAR");
		btnLimpar.setBackground(new Color(255, 255, 255));
		btnLimpar.setForeground(new Color(0, 175, 239));
		btnLimpar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				cnome.setText("");
				cdes.setText("");
				
			}
		});
		GroupLayout gl_sub = new GroupLayout(sub);
		gl_sub.setHorizontalGroup(
			gl_sub.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_sub.createSequentialGroup()
					.addGap(10)
					.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)
					.addGap(24)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 217, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_sub.createSequentialGroup()
					.addGap(4)
					.addComponent(btnSalvar, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
					.addGap(10)
					.addComponent(btnCancelar, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
					.addGap(10)
					.addComponent(btnLimpar, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
				.addGroup(gl_sub.createSequentialGroup()
					.addGap(4)
					.addGroup(gl_sub.createParallelGroup(Alignment.LEADING)
						.addComponent(label, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE))
					.addGap(24)
					.addGroup(gl_sub.createParallelGroup(Alignment.LEADING)
						.addComponent(cnome, GroupLayout.PREFERRED_SIZE, 217, GroupLayout.PREFERRED_SIZE)
						.addComponent(cbcategoria, GroupLayout.PREFERRED_SIZE, 217, GroupLayout.PREFERRED_SIZE))
					.addGap(46))
		);
		gl_sub.setVerticalGroup(
			gl_sub.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_sub.createSequentialGroup()
					.addGroup(gl_sub.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_sub.createSequentialGroup()
							.addGap(14)
							.addComponent(label_1))
						.addGroup(gl_sub.createSequentialGroup()
							.addContainerGap()
							.addComponent(cnome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(21)
					.addGroup(gl_sub.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_sub.createSequentialGroup()
							.addGap(3)
							.addComponent(label))
						.addComponent(cbcategoria, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(22)
					.addGroup(gl_sub.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_sub.createSequentialGroup()
							.addGap(6)
							.addComponent(label_2))
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 56, GroupLayout.PREFERRED_SIZE))
					.addGap(27)
					.addGroup(gl_sub.createParallelGroup(Alignment.LEADING)
						.addComponent(btnSalvar)
						.addComponent(btnCancelar)
						.addComponent(btnLimpar)))
		);
		sub.setLayout(gl_sub);
		
	}

	
	public Sub_Categoria getSubCategoria(){
		sc = new Sub_Categoria();
		Categoria ca = new Categoria();
		ca.nome = cbcategoria.getSelectedItem().toString();
		gapp.pesquisarCategoria(ca);
		
		sc.nome = cnome.getText();
		sc.obs = cdes.getText();
		sc.idcategoria = ca.id;
		
		return sc;
	}
	
	
	
	   public static ArrayList<Categoria> BuscarCategoria(){
		   gapp = new GerenciadorAplicacoes();
	   		ArrayList<Categoria> lista = gapp.buscarCategoria();
	   		
	   		return lista;
	   	}

	   
	public void AtualizarCategoria(){
		ArrayList<Categoria> al = BuscarCategoria();
		String[] dados = new String[al.size()]; 
		for (int i=0;i<al.size();i++){
			
			TelaCategoriaEstoque.c = al.get(i);
			dados[i] = TelaCategoriaEstoque.c.nome;
		}
		
		DefaultComboBoxModel combo = new DefaultComboBoxModel(dados);
		cbcategoria.setModel(combo);
		}

	
	public void salvar(){
		gapp.cadastrarSubCategoria(getSubCategoria());
	}
	
	
}
